package ICM.client;

import javax.swing.*;

import common.*;
import java.awt.event.*;
import java.util.*;

public class ClientUI extends JFrame implements Observer{
	
	public static final int LOGIN_SCREEN = 1;
	public static final int SETTINGS_SCREEN = 2;
	public static final int CREATE_USER = 3;
	public static final int Student_SCREEN = 4;
	public static int count=0;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int currentScreen = 0;
	private Client client = null;
	private User user = null;

	/**
	 * Constructor
	 */
	public ClientUI(Client c) {
		client = c;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("ICM Client");
		
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		JMenuItem exitMenu = new JMenuItem("Exit");
		exitMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		
		JMenuItem mntmCreateUser = new JMenuItem("Create User");
		fileMenu.add(mntmCreateUser);
		fileMenu.add(exitMenu);
		mntmCreateUser.addActionListener(new ActionListener() {
																public void actionPerformed(ActionEvent arg0) {
																	SetContent(CREATE_USER);
																}
															});
		
		JMenu optionsMenu = new JMenu("Options");
		menuBar.add(optionsMenu);
		
		JMenuItem settingsMenu = new JMenuItem("Settings");
		settingsMenu.addActionListener(new ActionListener() {
																public void actionPerformed(ActionEvent arg0) {
																	SetContent(SETTINGS_SCREEN);
																}
															});
		optionsMenu.add(settingsMenu);
		
		setJMenuBar(menuBar);
		
		pack();
		setSize(640,480);
	}
	
	/**
	 * Handle massage from Observable class in our case Client
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		String msg = "";
		if (arg1 instanceof TextMessage)
			msg = ((TextMessage)arg1).ToDebugMessage();
		else if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if(ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE)){
				String cmd=(String)(ms.getParams()[0]);
				if(cmd.equals(CommandMessage.LOGIN_MESSAGE))
				{
					if(ms.getParams()[1]==null)
					{
						ShowMessageBox("Error ", "Wrong credentials" ,JOptionPane.ERROR_MESSAGE);
					    count++;
					    if (count==3)
					    {
					    	ShowMessageBox(" Your entered wrong data 3 times "," DIE IN HELL  ;) " ,JOptionPane.ERROR_MESSAGE);
					    	count=0;
					    	return;
					    }
					}
					else
					{
						ShowMessageBox("Welcome", "Login success" ,JOptionPane.INFORMATION_MESSAGE);
						user=(User)ms.getParams()[1];
						SetContent(Student_SCREEN);
					}
				} 
				else if(cmd.equals(CommandMessage.CREATE_USER))
				{
					if((Integer.parseInt(ms.getParams()[1].toString()))==1)
					{
						ShowMessageBox("Information", "User was added" ,JOptionPane.INFORMATION_MESSAGE);
						SetContent(LOGIN_SCREEN);
					}
					else if((Integer.parseInt(ms.getParams()[1].toString()))==-1)
						ShowMessageBox("Error", "User already exists" ,JOptionPane.ERROR_MESSAGE);
					else
						ShowMessageBox("Error", "Can't add user" ,JOptionPane.ERROR_MESSAGE);
				}
			}
			else
			ShowMessageBox("OK", msg ,JOptionPane.INFORMATION_MESSAGE);
		}
		else {
			msg = arg1.toString();
			ShowMessageBox("Message Received", msg ,JOptionPane.INFORMATION_MESSAGE);
		}
		
	}
	
	public User getUser() {
		return user;
	}
	
	/**
	 * Show pop up message
	 * @param title Message title
	 * @param message Message content
	 * @param type Icon type
	 */
	public static void ShowMessageBox(String title, String message, int type) {
		JOptionPane.showMessageDialog(null, message, title, type);
	}
	
	public void SetContent(int screen) {
		if (currentScreen == screen)
			return;
		// Show appropriate screen
		switch (screen) {
			case LOGIN_SCREEN:
				// Login screen
				setContentPane(new LogInUI(client, this));
				break;
			case SETTINGS_SCREEN:
				SettingsUI s = new SettingsUI(this);
				s.BindSettings();
				setContentPane(s);
				break;
			case CREATE_USER:
				setContentPane(new CreateUserUI(client, this));
				break;
			case Student_SCREEN:
				setContentPane(new StudentWindow(client, this));
				break;
		}
		validate();
		currentScreen = screen;
	}
}
